import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../assets/color_config.dart';
import '../../common/const.dart';
import '../../common/router.dart';
import '../../page/main/home_page.dart';
import '../../page/main/mine_page.dart';
import '../../page/main/order_page.dart';
import '../../utils/common_page_config.dart';
import '../../utils/log_utils.dart';
import '../../utils/notifaction/notification_enter.dart';
import '../image_widget.dart';
import 'keep_alive_wrapper.dart';
import 'model/tab_model.dart';

class MyTabbar extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyTabbarState();
}

class _MyTabbarState extends State<MyTabbar> {
  TabModel model = TabModel();

  ///tabBar的数组
  List<Widget> _tabPageList = [];

  @override
  void initState() {
    super.initState();
    //监听进入order模块
    // NotificationCenter().addObserver(tabBarNotifyKey, widget, (value) {
    //   Future.delayed(const Duration(milliseconds: 1), () {
    //     model.changeBottomTabIndex(value);
    //     setState(() {});
    //   });
    // });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return getTabPageList(context: context)[index];
        },
        itemCount: getTabPageList(context: context).length,
        controller: model.tabPageController,
        physics: const NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: bottomNavigationWidget(),
    );
  }

  Widget bottomNavigationWidget() {
    return BottomNavigationBar(
      currentIndex: model.currentIndex,
      type: BottomNavigationBarType.fixed,
      selectedFontSize: 10.sp,
      unselectedFontSize: 10.sp,
      selectedItemColor: ColorConfig.colorPrimary,
      items: List<BottomNavigationBarItem>.generate(model.barItemList.length,
              (index) => _barItem(model.barItemList[index], model, index)),
      // 点击效果
      onTap: (int index) {
        didItemAction(index);
        logger("=====$index");
      },
    );
  }

  BottomNavigationBarItem _barItem(item, TabModel model, int index) {
    return BottomNavigationBarItem(
      icon: Padding(
        padding: const EdgeInsets.only(bottom: 5),
        child: ImageWidget(
          name: model.currentIndex == index
              ? item['selectIcon']
              : item['normalIcon'],
          width: 25.w,
          height: 25.w,
          color: model.presentColor(index),
          gaplessPlayback: true,
        ),
      ),
      label: item['title'],
    );
  }

  didItemAction(int index) {

    if (model.currentIndex != index) {
      model.changeBottomTabIndex(index);
      setState(() {});
      // NotificationCenter().postNotification(changeTabBarNotifyKey, index);
    }
  }


  //获取默认pageList
  List<Widget> getTabPageList({BuildContext? context}) {
    if (_tabPageList.isNotEmpty) {
      return _tabPageList;
    }
    if (context != null) {
      Widget page1 = decoratePageWidget(
          getWidgetByRouterName(context, Routers.home) ?? Container());
      Widget page2 = decoratePageWidget(
          getWidgetByRouterName(context, Routers.orderList) ?? Container());
      Widget page3 = decoratePageWidget(
          getWidgetByRouterName(context, Routers.mine) ?? Container());
      return _tabPageList = [page1, page2, page3];
    } else {
      return _tabPageList = [
        decoratePageWidget(HomePage()),
        decoratePageWidget(OrderPage()),
        decoratePageWidget(MinePage())
      ];
    }
  }

  //包裹pageWidget
  Widget decoratePageWidget(Widget page) {
    return KeepAliveWrapper(child: page);
  }

  @override
  void dispose() {
    super.dispose();
    NotificationCenter().removeOfKey(widget);
  }
}